package com.hospital.demo.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class LoginCheck {

    @Autowired
    private RedisUtil redisUtil;

    private static final int EXPIRE_TIME = 30 * 60;

    /**
     * Description: redis中不存在token
     * Author: Edarward
     * Date: 2019/3/20
     */
    public void login(String token) {
        if (!redisUtil.exist(token)) {
            redisUtil.set(token, 1, EXPIRE_TIME);

        }
    }

    /**
     * Description: 验证token是否有效
     * Author: Edarward
     * Date: 2019/4/18
     */
    public void checkToken(String token) throws Exception {
        if (StringUtils.isBlank(token)) {
            throw new Exception("没有token!");
        }
        if (!redisUtil.exist(token)) {
            throw new Exception("没有登陆!");
        }
        if (redisUtil.exist(token)) {
            redisUtil.set(token, 1, EXPIRE_TIME);
        }
    }

    /**
     * Description: 使token过期
     * Author: Edarward
     * Date: 2019/3/21
     */
    public void delToken (String token) throws Exception {
        if (StringUtils.isBlank(token)) {
            throw new Exception("没有token!");
        }
        if (redisUtil.exist(token)) {
            redisUtil.del(token);
        }
    }
}
